﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using Dapper;
using HongStone.Domain.SystemSetupModel.InstitutionSettingModel;
using HongStone.Ince;
using HongStone.Domain.AttendanceManagement;

namespace HongStone.Repository
{
    /// <summary>
    /// 登录首页的数据访问层
    /// </summary>
    public class LoginRepository:LoginInce
    {
        private readonly HongStoneContext _app;
        public LoginRepository(HongStoneContext app)
        {
            _app = app;
        }

        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddLogin(Login model)
        {
            using (IDbConnection conn = new SqlConnection("Data Source=10.31.60.27;Initial Catalog=HongShiDB;User ID=sa;pwd=123456"))
            {
                var list = conn.Execute($"insert into Login values('{model.LoginAccount}','{model.LoginName}','{model.PassWord}','{model.State}','{model.Email}')");
                return list;
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public List<Login> ValidateLogon(Login model)
        {
            using (IDbConnection conn = new SqlConnection("Data Source=10.31.60.27;Initial Catalog=HongShiDB;User ID=sa;pwd=123456"))
            {
                var list = conn.Query<Login>($"select * from Login where LoginAccount='{model.LoginAccount}' and PassWord='{model.PassWord}' ").ToList();
                return list;
            }
        }

        /// <summary>
        /// 找回密码
        /// </summary>
        /// <returns></returns>
        public List<Login> RetrievePassword(string LoginAccount,string LoginName)
        {
            using (IDbConnection conn = new SqlConnection("Data Source=10.31.60.27;Initial Catalog=HongShiDB;User ID=sa;pwd=123456"))
            {
                var list = $"select * from Login where 1=1 ";
                if (LoginAccount != "")
                {
                    list += $" and LoginAccount='{LoginAccount}' ";
                }
                if (LoginName != "")
                {
                    list += $" and LoginName='{LoginName}' ";
                }

                var page = conn.Query<Login>(list).ToList();

                return page;
            }
        }

        /// <summary>
        /// 显示个人信息
        /// </summary>
        /// <returns></returns>
        public List<Login> GetLogin(int LoginId)
        {
            using (IDbConnection conn = new SqlConnection("Data Source=10.31.60.27;Initial Catalog=HongShiDB;User ID=sa;pwd=123456"))
            {
                var list = conn.Query<Login>($"select * from Login where LoginId ='{LoginId}'").ToList();
                return list;
            }
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="LoginId"></param>
        /// <returns></returns>
        public int XiuGai(Login model)
        {
            using (IDbConnection conn = new SqlConnection("Data Source=10.31.60.27;Initial Catalog=HongShiDB;User ID=sa;pwd=123456"))
            {
                var list = conn.Execute($"update Login set PassWord='{model.PassWord}' where LoginId='{model.LoginId}'");

                return list;
            }
        }

        /// <summary>
        /// 锁屏
        /// </summary>
        /// <returns></returns>
        public List<Login> GetLoginName(string Name, string PassWork)
        {
            using (IDbConnection conn = new SqlConnection("Data Source=10.31.60.27;Initial Catalog=HongShiDB;User ID=sa;pwd=123456"))
            {
                var list = conn.Query<Login>($"select * from Login where PassWord='{PassWork}' and LoginName='{Name}'");

                return list.ToList(); ;
            }
        }

        /// <summary>
        /// 显示消息列表详细信息
        /// </summary>
        /// <returns></returns>
        public List<MessageApproval> GetMessageApproval(string NeiRong, int ZhuangTai,out int totalCount)
        {
            using (IDbConnection conn = new SqlConnection("Data Source=10.31.60.27;Initial Catalog=HongShiDB;User ID=sa;pwd=123456"))
            {
                var pages = conn.Query<MessageApproval>($"select * from MessageApproval").ToList();

                var list = $"select * from MessageApproval where 1=1 ";
                if (NeiRong != "")
                {
                    list += $" and NeiRong like '%{NeiRong}%'";
                }
                if (ZhuangTai == 1)
                {
                    list += " and ZhuangTai='未办'";
                }
                if (ZhuangTai == 2)
                {
                    list += " and ZhuangTai='未读'";
                }
                if (ZhuangTai == 3)
                {
                    list += " and ZhuangTai='已读'";
                }
                if (ZhuangTai == 4)
                {
                    list += " and ZhuangTai='已办'";
                }

                var page = conn.Query<MessageApproval>(list).ToList();

                totalCount = pages.Count();

                return page;
            }
        }

        /// <summary>
        /// 找回密码前修改密码为默认密码
        /// </summary>
        /// <returns></returns>
        public List<Login> XiuGai(string Email)
        {
            using (IDbConnection conn = new SqlConnection("Data Source=10.31.60.27;Initial Catalog=HongShiDB;User ID=sa;pwd=123456"))
            {
                var list = conn.Query<Login>($"update Login set PassWord='admin123' where Email='{Email}'");

                return list.ToList();
            }
        }
    }
}
